﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.ApplicationModel.Resources;

using SeriesManager.Model;
using SeriesManager.Enum;
using SeriesManager.Extension;

using WinRTFramework.Common;
using WinRTFramework.Interfaces.AppStates;

namespace SeriesManager.ViewModel
{
    public class AirViewModel : ViewModelBase, ISupportTransientState
    {
        #region Properties

        public ICollectionView View
        {
            get;
            private set;
        }

        #endregion

        #region DelegateProperties

        public ICommand ItemClickedCommand
        {
            get;
            private set;
        }

        #endregion

        public AirViewModel()
        {
            this.ItemClickedCommand = new DelegateCommand<ItemClickEventArgs>(OnItemClickExecuted);
        }

        #region DelegatePropertiesImplementation

        private void OnItemClickExecuted(ItemClickEventArgs e)
        {
            var clickedItem = e.ClickedItem as EpisodeModel;
            var arr = new int[] { clickedItem.Season.Series.ID, clickedItem.ID };
            base.NavigationService.Navigate<EpisodeViewModel>(arr);
        }

        #endregion

        public override void OnNavigatedTo(object navigationParameter)
        {
            base.OnNavigatedTo(navigationParameter);

            var res = new ResourceLoader();
            switch ((int)navigationParameter)
            {
                case -4:
                    this.View = new CollectionViewSource() { Source = AppX.Instance.AirYear.GroupBy(E => (MonthOfYear)E.FirstAired.Date.Month), IsSourceGrouped = true }.View;
                    break;
                case -3:
                    this.View = new CollectionViewSource() { Source = AppX.Instance.AirMonth.GroupBy(E => (WeekOfMonth)E.FirstAired.Date.GetWeekOfMonth()), IsSourceGrouped = true }.View;
                    break;
                case -2:
                    this.View = new CollectionViewSource() { Source = AppX.Instance.AirWeek.GroupBy(E => E.FirstAired.Date.DayOfWeek), IsSourceGrouped = true }.View;
                    break;
                case -1:
                    this.View = new CollectionViewSource() { Source = new List<AirModel>(new AirModel[] { new AirModel(res.GetString("AirToday"), AppX.Instance.AirDay) }), IsSourceGrouped = true }.View;
                    break;
                case 0:
                    var airCol = new AirModel[] {
                        new AirModel(res.GetString("AirWeek"), AppX.Instance.Current.AirWeek),
                        new AirModel(res.GetString("AirMonth"), AppX.Instance.Current.AirMonth),
                        new AirModel(res.GetString("AirYear"), AppX.Instance.Current.AirYear),
                        new AirModel(res.GetString("AirAll"), AppX.Instance.Current.AirOther)
                    };
                    this.View = new CollectionViewSource() { Source = airCol, IsSourceGrouped = true }.View;
                    break;
            }
            base.OnPropertyChanged<ICollectionView>(() => this.View);
        }

        public object GetTransientState()
        {
            if (this.View != null && this.View.CurrentItem != null)
            {
                return (this.View.CurrentItem as EpisodeModel).ID;
            }
            return null;
        }

        public IEnumerable<Type> GetTransientStateTypes()
        {
            yield return typeof(int);
        }

        public void UpdateTransientState(object state)
        {
            var currentItem = this.View.FirstOrDefault(E => (E as EpisodeModel).ID == (int)state);
            if (currentItem != null)
            {
                this.View.MoveCurrentTo(currentItem);
            }
        }
    }
}
